package com.qx.service;

import com.qx.mapper.ScAddressMapper;
import com.qx.service.ScAddressService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qx.pojo.ScAddress;

@Service
public class ScAddressServiceImpl extends ServiceImpl<ScAddressMapper, ScAddress> implements ScAddressService {

    @Resource
    private ScAddressMapper scAddressMapper;

    @Override
    public ScAddress getDefaultAddress(Integer id) {
        List<ScAddress> scAddress = scAddressMapper.getDefaultAddress(id);
        if (Objects.nonNull(scAddress)){
            List<ScAddress> addresses = scAddress.stream()
                    .filter((address) -> {
                        return address.getDefa() == 1;
                    }).collect(Collectors.toList());
            if (addresses.isEmpty()){
                return scAddress.get(0);
            }
        }
        return null;
    }
}
